package com.masterclient.app.modules.purchase.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselibrary.bean.order.ConfirmOrderBean;
import com.eoner.baselibrary.bean.purchase.PurchaseCartData;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * @auther hjian
 * @date 2020/9/23.
 */
public class PurchaseShopCartPresenter extends PurchaseShopCartContract.Presenter {
    @Override
    public void getData(String cart_ids) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cart_ids",cart_ids);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().purchaseCartList(body), new Consumer<CommonBaseResponse<PurchaseCartData>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<PurchaseCartData> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showData(bean.getData());
                }else {
                    mView.showFailure(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showFailure("网络异常，请重试");
            }
        }));
    }

    @Override
    public void updateData(String cart_id, String num) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cart_id",cart_id);
            jsonObject.put("num",num);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().purchaseCartUpdate(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(@NonNull CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.operationalResult(true,0,num);
                }else {
                    mView.operationalResult(false,0,bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.operationalResult(false,0,"网络异常，请重试");
            }
        }));
    }

    @Override
    public void deleteData(String cart_ids) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cart_ids",cart_ids);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().purchaseCartDel(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(@NonNull CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.operationalResult(true,1,"");
                }else {
                    mView.operationalResult(false,1,bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.operationalResult(false,1,"网络异常，请重试");
            }
        }));
    }

    @Override
    public void confirmOrder(String buy_product, String buy_type, String is_custody) {

        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject buyProduct = new JSONObject(buy_product);
            jsonObject.put("buy_product",buyProduct);
            jsonObject.put("buy_type",buy_type);
            jsonObject.put("sale_type","4");
            jsonObject.put("is_custody",is_custody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ConfirmOrderBean> observable = RetrofitUtil2.getInstance().confirmOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmOrderBean>() {
            @Override
            public void accept(@NonNull ConfirmOrderBean bean) throws Exception {
                if ("0".equals(bean.getCode())) {
                    mView.confirmOrderSuccess(bean.getData(),is_custody);
                } else {
                    mView.showFailure(bean.getMsg());
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.showFailure("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}
